package com.wzxy.wzinfo.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;


import com.wzxy.wzinfo.validate.CustomUserService;

/**
 * Created by pan on 2017/5/8.
 */
@Configurable
@EnableWebSecurity// 注解开启Spring Security的功能
//@EnableGlobalMethodSecurity(prePostEnabled = true)//开启security注解
public class MyWebSecurityConfig extends WebSecurityConfigurerAdapter{
    //继承 WebSecurityConfigurerAdapter ，并重写它的方法来设置一些web安全的细节

	@Autowired
	private CustomUserService customUserService;
	
    @Override
    protected void configure(HttpSecurity http) throws Exception {
    	http.headers().frameOptions().disable();
        http
                .authorizeRequests()
                .antMatchers("/","/static/**").permitAll()
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .loginPage("/User/openpage?page=login")
                .loginProcessingUrl("/login")//
                .usernameParameter("username")//设置用户名参数 默认username
                .passwordParameter("password")//设置密码参数  默认password
                //.defaultSuccessUrl("/action/Login_login.action")//登录成功后默认跳转到"/hello"
                .successHandler(new SimpleLoginSuccessHandler())//和defaultSuccessUrl只能有一个
                .failureUrl("/User/openpage?page=login?msg=failure")
                .permitAll()
                .and()
                .logout()
                .permitAll()
                .and();
        http.csrf().disable();
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(customUserService);
    }

    /**
     * 设置用户密码的加密方式为MD5加密
     * @return
     */
//    @Bean
//    public Md5PasswordEncoder passwordEncoder() {
//        return new Md5PasswordEncoder();
//    }

    /**
     * 自定义UserDetailsService，从数据库中读取用户信息
     * @return
     */
//    @Bean
//    public CustomUserService customUserService(){
//        return new CustomUserService();
//    }

}

